#!/bin/bash
cd /
FILE=/usr/script/ip_apnic
mv /usr/script/cn.net2 /usr/script/cn.net2.old
mv /usr/script/cn.net /usr/script/cn.net.old
mv $FILE /usr/script/ip_apnic.old
rm -f /usr/apmd5.remote
rm -f /var/www/html/delegated-apnic-latest
aria2c -c -x 10 -s 100 http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest.md5 ftp://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest.md5 -o /usr/apmd5.remote
cp /usr/apmd5.remote /var/www/html/delegated-apnic-latest.md5
sed -i "s/^...............................\(.*\)$/\1/g" /usr/apmd5.remote
aria2c -c -x 10 -s 100 http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest ftp://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -o /var/www/html/delegated-apnic-latest
cp /var/www/html/delegated-apnic-latest $FILE
find /usr/script/ip_apnic |xargs md5sum|sort|sed "s/^\(.*\)......................$/\1/g" > /usr/apmd5.local

lt=$(sed -n 28p /usr/script/ip_apnic|awk -F"|" '{print $4}')
ltt=$(($lt+31))
ltf=$(cat /usr/script/ip_apnic|wc -l)
if [ $ltt -ne $ltf ];then
echo $(date "+%Y/%m/%d/%T") Line Number not match!Recovery old file
        cp /usr/script/ip_apnic.old $FILE
        cp /usr/script/cn.net2.old /usr/script/cn.net2
        cp /usr/script/cn.net.old /usr/script/cn.net
        exit
else
        diff -r  /usr/apmd5.local /usr/apmd5.remote
        if [ $? -ne 0 ];then
        echo $(date "+%Y/%m/%d/%T") Data check error! Recovery old file
        cp /usr/script/ip_apnic.old $FILE
        cp /usr/script/cn.net2.old /usr/script/cn.net2
        cp /usr/script/cn.net.old /usr/script/cn.net
        exit
        else
chmod 755 $FILE
echo $(date "+%Y/%m/%d/%T") Data Check OK! Translating
grep 'apnic|CN|ipv4|' $FILE | cut -f 4,5 -d'|'|sed -e 's/|/ /g' | while read ip cnt
do
        echo $ip:$cnt
        mask=$(cat << EOF | bc | tail -1
pow=32;
define log2(x) {
if (x<=1) return (pow);
pow--;
return(log2(x/2));
}
log2($cnt)
EOF
)
        echo "ipset add ssh" $ip/$mask >>/usr/script/cn.net
                echo $ip/$mask >>/usr/script/cn.net2
              done

              echo $(date "+%Y/%m/%d/%T") Update Done
                      fi
                    fi

                    cp /usr/script/cn.net2 /gitee/wyg3/ignore.list
cd /gitee/wyg3
ssh  -o "StrictHostKeyChecking no" -T git@gitee.com
git remote set-url origin git@gitee.com:ipupdate/wyg3.git
cp /usr/script/iprecord_h /gitee/wyg3/iprecord_h
cp /usr/script/ignore.list /gitee/wyg3/ignore.list
git add ignore.list
uptime=$(date '+%Y%m%d%H%M')
git commit -m "$uptime"
git pull origin master
git push

mv /usr/script/CMNET /usr/script/CMNET.old
touch /usr/script/CMNET
chmod 755 /usr/script/CMNET
linecnt=0
lineno=$(cat /usr/script/cn.net2|wc -l)
while (( $linecnt < $lineno ))
do
  (( linecnt++ ))
  ip=$(sed -n "$linecnt"p /usr/script/cn.net2)
#  mask=$(sed -n "$linecnt"p /usr/script/cn.net2 | cut -d '/' -f 2)
  NETNAME=`whois $ip | sed -e '/./{H;$!d;}' -e 'x;/netnum/!d' |grep ^netname | sed -e 's/.*:      \(.*\)/\1/g' | sed -e 's/-.*//g'| sed 's/^[ \t]*//g'`
        case $NETNAME in
#        UNICOM)
#                echo $ip/$mask >> /usr/script/UNICOM
#        ;;
        CMNET)
                echo $ip >> /usr/script/CMNET
        ;;
        CTTNET)
                echo $ip >> /usr/script/CMNET
        ;;
        "")
                echo $ip >> /usr/script/TIMEOUT
        ;;
        $NETNAME)
                echo $ip >> /usr/script/TEST/$NETNAME
        ;;
        esac
   echo "$ip $NETNAME"
done
